package cn.kooki.app.duobao.ui.Activity.Goods;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.Bean.Index.IndexGoodResponse;
import cn.kooki.app.duobao.data.bus.AddSearchShopEvent;
import cn.kooki.app.duobao.data.bus.AddShopEvent;
import cn.kooki.app.duobao.data.constant.Constants;
import cn.kooki.app.duobao.ui.Activity.Goods.GoodsDetailAcitvity;
import cn.kooki.app.duobao.ui.Adapter.SearchDetailResultAdapter;
import cn.kooki.app.duobao.ui.listener.AddShopCartCallBack;
import cn.kooki.app.duobao.ui.view.CustomRefreshHeader;
import cn.kooki.app.duobao.ui.view.LoadMoreContainer;
import cn.kooki.app.duobao.ui.view.LoadMoreHandler;
import cn.kooki.app.duobao.ui.view.LoadMoreListViewContainer;
import cn.kooki.app.duobao.ui.widget.CustomRelativeLayout;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.manager.ShopCartManager;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xionghao on 15/12/11.
 */
public class SearchResultActivity extends BaseNavActivity implements View.OnClickListener {


    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.swipe_refresh_layout)
    PtrFrameLayout swipeRefreshLayout;
    @Bind(R.id.loading_wrapper)
    CustomRelativeLayout loadingWrapper;
    @Bind(R.id.load_more_content_container)
    LoadMoreListViewContainer loadMoreContentContainer;
    private String type;
    private SearchDetailResultAdapter adapter;

    private ArrayList<GoodsItem> datas = new ArrayList<>();
    private String title;
    private int page = 1;

    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.include_list_view_with_loadmore_fragment);
        ButterKnife.bind(this);
    }

    @Override
    protected void afterView() {
        loadingWrapper.showloading();
        type = getIntent().getStringExtra("type");
        title = getIntent().getStringExtra("title");
        setTitle(title);
        actionEdit.setText(R.string.add_all);
        swipeRefreshLayout.setVisibility(View.VISIBLE);
        adapter = new SearchDetailResultAdapter(this, datas);
        CustomRefreshHeader refreshHeader = new CustomRefreshHeader(this);
        swipeRefreshLayout.addPtrUIHandler(refreshHeader);
        swipeRefreshLayout.setHeaderView(refreshHeader);
        swipeRefreshLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getdata(false);
            }

        });
        loadMoreContentContainer.setAutoLoadMore(true);
        loadMoreContentContainer.useDefaultFooter();

        loadMoreContentContainer.setShowLoadingForFirstPage(true);

        loadMoreContentContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                getdata(true);
            }
        });

        actionEdit.setOnClickListener(this);
        adapter.setCartCallBack(new AddShopCartCallBack() {
            @Override
            public void excuteAnimi(AddSearchShopEvent shopEvent) {

            }
        });
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GoodsItem item = datas.get(position);
                HashMap<String, String> params = new HashMap<>();
                params.put("id", item.getId());
                params.put("qishu", item.getQishu());
                intent(GoodsDetailAcitvity.class, params);
            }
        });
        getdata(false);

    }

    private void getdata(final boolean b) {
        int typeid = StringUtils.nullSafeParseInt(type);
        if (b) {
            page++;
        } else {
            page = 1;
        }
        HashMap<String, Object> other = new HashMap<>();
        if (typeid != 10) {
            other.put("cid", typeid);
        } else {
            other.put("gonum", typeid);
        }
        MainFactory.getApiInstance().goodRespone(page, other, new Callback<IndexGoodResponse>() {
            @Override
            public void success(IndexGoodResponse indexGoodResponse, Response response) {
                hideloading();
                System.out.println(response.getBody().toString());
                if (indexGoodResponse.getData() != null) {
                    if (!b) {
                        datas.clear();
                    }
                    datas.addAll(indexGoodResponse.getData());
                    adapter.notifyDataSetChanged();
                    loadMoreContentContainer.loadMoreFinish(datas.size() == 0, indexGoodResponse.getData().size() == Constants.DEFAULT_COUNT);
                    setTitle(title + "(" + datas.size() + ")");
                    actionEdit.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                hideloading();

            }
        });
    }

    private void hideloading() {
        loadingWrapper.hideloading();
        swipeRefreshLayout.refreshComplete();
    }


    @Override
    public void onClick(View v) {
        if (v == actionEdit) {
            if (datas.size() > 0) {
                ShopCartManager.getInstance().addShopCarts(datas);
                showToastMessage("添加成功");
            }
        }
    }


}
